USE [ATAChietkhau]
GO

/****** Object:  StoredProcedure [dbo].[sprTongHopPhanTichDoanhSoQuyReport]    Script Date: 07/18/2010 10:59:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




ALTER PROCEDURE [dbo].[sprXacNhanCongNoReport]
	-- Add the parameters for the stored procedure here
	@AccountingObjectId uniqueidentifier, 
	@Month INT,
	@Year INT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	--DECLARE @vReportDate DATETIME
	--SELECT @vReportDate = dbo.Date(@Year, @Month, 1)

	--DECLARE @TrungTamName NVARCHAR(255)
	--DECLARE @TrungTamCode NVARCHAR(20)

	--SELECT @TrungTamName = BranchName, @TrungTamCode = BranchCode FROM vwMisaBranches WHERE BranchID = @BranchID
	
	--DECLARE @BrandName NVARCHAR(50)
	--DECLARE @BrandCode NVARCHAR(50)
	--SELECT @BrandCode = BrandCode, @BrandName = BrandName FROM dbo.ThuongHieu WHERE BrandID = @BrandID

    -- Insert statements for procedure here
	CREATE TABLE #tblXacNhanCongNoReport
	(
		KhachHangID UNIQUEIDENTIFIER,
		MaKH NVARCHAR(100),
		TenKH NVARCHAR(255),
		LEVEL INT,
		ParentKhachHangID UNIQUEIDENTIFIER,
		DiaChi NVARCHAR(1000),
		DienThoai NVARCHAR(200),
		Thang INT,
		Nam INT,
		CongNoDauKy DECIMAL,
		PhaiThuTuBanHang DECIMAL,
		PhaiThuKhac DECIMAL,
		ThanhToanTrongKy DECIMAL,
		TongTraThuong DECIMAL,
		CongNoLuyKeNam DECIMAL,
		CongNoQuaHanThanhToan DECIMAL
	)


	INSERT INTO #tblXacNhanCongNoReport(KhachHangID, MaKH, TenKH, LEVEL, ParentKhachHangID, Thang, Nam, DiaChi, DienThoai)
	SELECT D.KhachHangID, D.MaKH, D.TenKH, D.Level, D.ParentID AS ParentKhachHangID, @Month AS Thang, @Year AS Nam, D.Address, D.Tel
	FROM vwAllCustomers D 
	WHERE D.KhachHangID = @AccountingObjectId
	
	--SELECT D.KhachHangID, D.MaKH, D.TenKH, D.Level, D.ParentID AS ParentKhachHangID, D.Tel, D.Address
	--FROM vwAllCustomers D 
	--WHERE D.KhachHangID = '64B9E791-ECF5-4AF0-A749-CDEE005BB714'

	SELECT * FROM #tblXacNhanCongNoReport

	DROP TABLE #tblXacNhanCongNoReport

	-- exec sprXacNhanCongNoReport '64B9E791-ECF5-4AF0-A749-CDEE005BB714', 2, 2010
END




GO


